--- title: "Map of The Republic of Serbia - Net Earnings Sep,2019" author: "Igor Popovic" date: 2020-02-24 categories: ["R"] tags: ["R Markdown", "Serbia", "Earnings", "Map"] fig.width: 9 code_folding: "hide" ---

Data import

First step for map creation is to have the longitude and latitude information for a country. The longitude and latitude form a dot on a map, but several of them can form, in mathematics called, polygonal line. Process is very simple, we will have in our data frame list of longitudes and latitudes and we will connect the dots with straight line, which mean more dots, the map will look more representative.

You can check more data frames for other countries on this webpage

All the data I have stored to an excel file called districtsSerbia.xlsx and for this step you need package called readxl, or you can use any other package from which you can read data from excel (package xlsx for example). Name of the file is under quote marks, because the file is in the same folder as the r markdown file. If the path file is different you must insert the full path to the file (for example “C/Users/Desktop/filename.xlsx”). fortify function you can find in package for ploting called ggplot2.


library(readxl)
library(ggplot2)

serbia.adm2.shp.df = fortify(data.frame(read_excel("districtsSerbia.xlsx")))

head(serbia.adm2.shp.df)
##       long      lat order  hole piece id     id_name id_name_vis group
## 1 20.38908 44.29429     1 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 2 20.38950 44.29555     2 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 3 20.39240 44.29978     3 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 4 20.39747 44.30449     4 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 5 20.39903 44.30624     5 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 6 20.40010 44.30808     6 FALSE     1  0 Aranđelovac Aranđelovac   0.1

This is how the data frame looks like:


head(serbia.adm2.shp.df)
##       long      lat order  hole piece id     id_name id_name_vis group
## 1 20.38908 44.29429     1 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 2 20.38950 44.29555     2 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 3 20.39240 44.29978     3 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 4 20.39747 44.30449     4 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 5 20.39903 44.30624     5 FALSE     1  0 Aranđelovac Aranđelovac   0.1
## 6 20.40010 44.30808     6 FALSE     1  0 Aranđelovac Aranđelovac   0.1

Data merging

Next thing is to connect the mapping data with earnings data that I have by each municipality. Source of the net earnings you can find on website Statistical Office of the Republic of Serbia. Merging the data for mapping and net earnings can be done with function merge. For this you need package dplyr. Data frames are merged by column id which must be identical.

Net earnings have currency serbian dinar, so I transformed them to EUR currency.


library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

NetEarn = data.frame(read_excel("NetEarnSep2019.xlsx"))

head(NetEarn)
##   id     id_name  NETO
## 1  0 Aranđelovac 47069
## 2  1    Batočina 41498
## 3  2        Knić 43394
## 4  3  Kragujevac 50974
## 5  4      Lapovo 43167
## 6  5        Rača 42919

serbia.adm2.shp.df = merge(serbia.adm2.shp.df,NetEarn,by="id")

serbia.adm2.shp.df = cbind(serbia.adm2.shp.df, Euro = round(serbia.adm2.shp.df$NETO/117.5962,2))

# 1 EUR = 117.5962 and that value is rounded on 2 decimal places

head(serbia.adm2.shp.df)
##   id     long      lat order  hole piece   id_name.x id_name_vis group
## 1  0 20.38908 44.29429     1 FALSE     1 Aranđelovac Aranđelovac   0.1
## 2  0 20.38950 44.29555     2 FALSE     1 Aranđelovac Aranđelovac   0.1
## 3  0 20.39240 44.29978     3 FALSE     1 Aranđelovac Aranđelovac   0.1
## 4  0 20.39747 44.30449     4 FALSE     1 Aranđelovac Aranđelovac   0.1
## 5  0 20.39903 44.30624     5 FALSE     1 Aranđelovac Aranđelovac   0.1
## 6  0 20.40010 44.30808     6 FALSE     1 Aranđelovac Aranđelovac   0.1
##     id_name.y  NETO   Euro
## 1 Aranđelovac 47069 400.26
## 2 Aranđelovac 47069 400.26
## 3 Aranđelovac 47069 400.26
## 4 Aranđelovac 47069 400.26
## 5 Aranđelovac 47069 400.26
## 6 Aranđelovac 47069 400.26

Map

And finnaly mapping. Only packages ggplot2, viridis for coloring the map are used in this process.


library(viridis)
## Loading required package: viridisLite

# Defining the ranges of salaries
serbia.adm2.shp.df$brksZ = cut(serbia.adm2.shp.df$Euro, breaks=c(-1,250,300,350,400,450,500,550,600,650,700,750,800,10000), labels=c("< 250 €","250 - 300 €","300 - 350 €","350 - 400 €","400 - 450 €","450 - 500 €","500 - 550 €","550 - 600 €","600 - 650 €","650 - 700 €","700 - 750 €","750 - 800 €","> 800 €"))



mapaSRB = ggplot(data=serbia.adm2.shp.df, aes(x=long, y=lat, group=group)) +
          geom_path() + 
          geom_polygon(aes(fill=brksZ), color = 'white', size = 0.75) +
          scale_fill_viridis(option = "magma", name="Average net earnings", discrete=T, na.value="#969696", 
                             guide = guide_legend(keyheight = unit(5, units = "mm"),
                                                  title.position = 'top',
                                                  reverse = F
                                                  ),  direction = -1) +
          theme(
              text = element_text(color = "#ffffff", size=15),
              axis.line = element_blank(),
              axis.text.x = element_blank(),
              axis.text.y = element_blank(),
              axis.ticks = element_blank(),
              axis.title.x = element_blank(),
              axis.title.y = element_blank(),
              panel.grid.major = element_line(color = "#6A6A6A", size = 0.2),
              panel.grid.minor = element_blank(),
              plot.background = element_rect(fill = "#6A6A6A", color = NA),
              panel.background = element_rect(fill = "#6A6A6A", color = NA),
              legend.background = element_rect(fill = "#6A6A6A", color = NA),
              legend.position=c(0.85, 0.85),
              panel.border = element_blank()) + 
        labs(x = NULL, y = NULL, title = "Republic of Serbia", subtitle = "Net earnings by municipalities and cities,\nSeptember 2019", caption = "Author: Igor Popovic,\n Source: Statistical Office of the Republic of Serbia")
 
mapaSRB


Interactive map


# Name of municipalities
adm.names = aggregate(cbind(long,lat)~group+id_name_vis, serbia.adm2.shp.df, FUN=function(x)mean(range(x)))

library(plotly)

# Defining the ranges of salaries
serbia.adm2.shp.df$brksZ = cut(serbia.adm2.shp.df$Euro, breaks=c(-1,250,300,350,400,450,500,550,600,650,700,750,800,10000), labels=c("< 250 €","250 - 300 €","300 - 350 €","350 - 400 €","400 - 450 €","450 - 500 €","500 - 550 €","550 - 600 €","600 - 650 €","650 - 700 €","700 - 750 €","750 - 800 €","> 800 €"))



mapaSRB = ggplot(data=serbia.adm2.shp.df, aes(x=long, y=lat, group=group, text=paste0("Municipality: ",id_name.x,"<br>", "Net Earnings: ", serbia.adm2.shp.df$Euro, " EUR"))) +
          geom_path() + 
          geom_polygon(aes(fill=brksZ), color = 'white', size = 0.75) +
          scale_fill_viridis(option = "magma", name="Average net earnings", discrete=T, na.value="#969696", 
                             guide = guide_legend(keyheight = unit(5, units = "mm"),
                                                  title.position = 'top',
                                                  reverse = F
                                                  ),  direction = -1) +
          theme(
              text = element_text(color = "#ffffff", size=15),
              axis.line = element_blank(),
              axis.text.x = element_blank(),
              axis.text.y = element_blank(),
              axis.ticks = element_blank(),
              axis.title.x = element_blank(),
              axis.title.y = element_blank(),
              panel.grid.major = element_line(color = "#6A6A6A", size = 0.2),
              panel.grid.minor = element_blank(),
              plot.background = element_rect(fill = "#6A6A6A", color = NA),
              panel.background = element_rect(fill = "#6A6A6A", color = NA),
              legend.background = element_rect(fill = "#6A6A6A", color = NA),
              legend.position=c(0.85, 0.85),
              panel.border = element_blank()) + 
        labs(x = NULL, y = NULL, title = "Republic of Serbia", subtitle = "Net earnings by municipalities and cities,\nSeptember 2019", caption = "Author: Igor Popovic,\n Source: Statistical Office of the Republic of Serbia")
 
ggplotly(mapaSRB, tooltip="text")